<template>
  <div v-loading="!dataListShow" element-loading-text="数据加载中..." :style="!dataListShow ? 'height: 500px' : 'height: 100%'">
    <div v-if="dataListShow">
      <el-card class="box-card">
        <div style="color: #666; font-size: 13px">
          <span> 系统：{{ dataList.sys.os }} </span>
          <span> IP：{{ dataList.sys.ip }} </span>
          <span> 项目已不间断运行：{{ dataList.sys.day }} </span>
          <i
            class="el-icon-refresh"
            style="margin-left: 40px"
            @click="getDataList()"
          />
        </div>
      </el-card>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span style="font-weight: bold; color: #666; font-size: 15px">状态</span>
        </div>
        <div>
          <el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6"
            style="margin-bottom: 10px"
          >
            <div class="title">CPU使用率</div>
            <el-tooltip placement="top-end">
              <div slot="content" style="font-size: 12px">
                <div style="padding: 3px">
                  {{ dataList.cpu.name }}
                </div>
                <div style="padding: 3px">{{ dataList.cpu.package }}</div>
                <div style="padding: 3px">{{ dataList.cpu.core }}</div>
                <div style="padding: 3px">{{ dataList.cpu.logic }}</div>
              </div>
              <div class="content">
                <el-progress type="dashboard" :percentage="parseFloat(dataList.cpu.used)" />
              </div>
            </el-tooltip>
            <div class="footer">{{ dataList.cpu.coreNumber }} 核心</div>
          </el-col>
          <el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6"
            style="margin-bottom: 10px"
          >
            <div class="title">内存使用率</div>
            <el-tooltip placement="top-end">
              <div slot="content" style="font-size: 12px">
                <div style="padding: 3px">总量：{{ dataList.memory.total }}</div>
                <div style="padding: 3px">已使用：{{ dataList.memory.used }}</div>
                <div style="padding: 3px">空闲：{{ dataList.memory.available }}</div>
              </div>
              <div class="content">
                <el-progress type="dashboard" :percentage="parseFloat(dataList.memory.usageRate)" />
              </div>
            </el-tooltip>
            <div class="footer">{{ dataList.memory.used }} / {{ dataList.memory.total }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6"
            style="margin-bottom: 10px"
          >
            <div class="title">交换区使用率</div>
            <el-tooltip placement="top-end">
              <div slot="content" style="font-size: 12px">
                <div style="padding: 3px">总量：{{ dataList.swap.total }}</div>
                <div style="padding: 3px">已使用：{{ dataList.swap.used }}</div>
                <div style="padding: 3px">空闲：{{ dataList.swap.available }}</div>
              </div>
              <div class="content">
                <el-progress type="dashboard" :percentage="parseFloat(dataList.swap.usageRate)" />
              </div>
            </el-tooltip>
            <div class="footer">{{ dataList.swap.used }} / {{ dataList.swap.total }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6"
            style="margin-bottom: 10px"
          >
            <div class="title">磁盘使用率</div>
            <div class="content">
              <el-tooltip placement="top-end">
                <div slot="content" style="font-size: 12px">
                  <div style="padding: 3px">总量：{{ dataList.disk.total }}</div>
                  <div style="padding: 3px">空闲：{{ dataList.disk.available }}</div>
                </div>
                <div class="content">
                  <el-progress type="dashboard" :percentage="parseFloat(dataList.disk.usageRate)" />
                </div>
              </el-tooltip>
            </div>
            <div class="footer">{{ dataList.disk.used }} / {{ dataList.disk.total }}</div>
          </el-col>
        </div>
      </el-card>
      <div>
        <el-row :gutter="6">
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" style="margin-bottom: 10px">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span style="font-weight: bold;color: #666;font-size: 15px">CPU使用率监控</span>
              </div>
              <div>
                <v-chart :options="cpuInfo" />
              </div>
            </el-card>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" style="margin-bottom: 10px">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span style="font-weight: bold;color: #666;font-size: 15px">内存使用率监控</span>
              </div>
              <div>
                <v-chart :options="memoryInfo" />
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import ECharts from 'vue-echarts'
import 'echarts/lib/chart/line'
import 'echarts/lib/component/polar'
export default {
  data() {
    return {
      dataList: {},
      serverMonitor: null,
      dataListShow: false,
      cpuInfo: {
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: []
        },
        yAxis: {
          type: 'value',
          min: 0,
          max: 100,
          interval: 20
        },
        series: [{
          data: [],
          type: 'line',
          areaStyle: {
            normal: {
              color: 'rgb(32,160,255)'
            }
          },
          itemStyle: {
            normal: {
              color: '#6fbae1',
              lineStyle: {
                color: '#6fbae1'
              }
            }
          }
        }]
      },
      memoryInfo: {
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: []
        },
        yAxis: {
          type: 'value',
          min: 0,
          max: 100,
          interval: 20
        },
        series: [{
          data: [],
          type: 'line',
          areaStyle: {
            normal: {
              color: 'rgb(32, 160, 255)' // 改变区域颜色
            }
          },
          itemStyle: {
            normal: {
              color: '#6fbae1',
              lineStyle: {
                color: '#6fbae1' // 改变折线颜色
              }
            }
          }
        }]
      }

    }
  },
  components: {
    'v-chart': ECharts
  },
  created() {
    this.getDataList();
    this.serverMonitor =  window.setInterval(() => {
      setTimeout(() => {
        this.getDataList()
      },2)
    },3500)
  },
  destroyed() {
    clearInterval(this.serverMonitor)
  },
  methods: {
    getDataList() {
      this.$http({
        url: this.$http.adornUrl('/mnt/server/list'),
        method: 'get',
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.list
          this.dataListShow = true
          if (this.cpuInfo.xAxis.data.length >= 8) {
            this.cpuInfo.xAxis.data.shift()
            this.memoryInfo.xAxis.data.shift()
            this.cpuInfo.series[0].data.shift()
            this.memoryInfo.series[0].data.shift()
          }
          this.cpuInfo.xAxis.data.push(data.list.time)
          this.memoryInfo.xAxis.data.push(data.list.time)
          this.cpuInfo.series[0].data.push(parseFloat(data.list.cpu.used))
          this.memoryInfo.series[0].data.push(parseFloat(data.list.memory.usageRate))
        }
      }).catch((err) => {
        console.log(err);
      });
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.box-card {
  margin-bottom: 5px;
  span {
    margin-right: 28px;
  }
  .el-icon-refresh {
    margin-right: 10px;
    float: right;
    cursor: pointer;
  }
}
.cpu,
.memory,
.swap,
.disk {
  width: 20%;
  float: left;
  padding-bottom: 20px;
  margin-right: 5%;
}
.title {
  text-align: center;
  font-size: 15px;
  font-weight: 500;
  color: #999;
  margin-bottom: 16px;
}
.footer {
  text-align: center;
  font-size: 15px;
  font-weight: 500;
  color: #999;
  margin-top: -5px;
  margin-bottom: 10px;
}
.content {
  text-align: center;
  margin-top: 5px;
  margin-bottom: 5px;
}
</style>
